package com.yh.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String url;
    private static String driver;
    private static String username;
    private static String password;
    private static Connection con;

    static {
        Properties properties = new Properties();
        try (InputStream is = JDBCUtil.class.getResourceAsStream("/jdbc.properties")){
            properties.load(is);
            url = properties.getProperty("url");
            driver = properties.getProperty("driver");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取链接
     * @return
     */
    private Connection getConnection(){
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭链接
     */
    public void closeConnection(){
        try {
            con.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 查询全部，按条件查询
     * @param sql
     * @param params
     * @return
     */
    public ResultSet Find(String sql, Object... params){
        ResultSet result = null;
        try {
            PreparedStatement ps = this.getConnection().prepareStatement(sql);
            if (params != null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }
            result = ps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    /**
     * 查询单行单列
     * @param sql
     * @param cls
     * @param params
     * @param <T>
     * @return
     */
    public <T> T FindOne(String sql, Class<T> cls, Object... params){
        T result = null;
        ResultSet resultSet = null;
        try {
            PreparedStatement ps = this.getConnection().prepareStatement(sql);
            if (params != null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }
            resultSet = ps.executeQuery();
            if (resultSet.next()){
                result = resultSet.getObject(1,cls);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    /**
     * 通用增删改
     * @param sql
     * @param params
     * @return
     */
    public int ExcuteUpdate(String sql,Object... params){
        int result = -1;
        try {
            PreparedStatement ps = this.getConnection().prepareStatement(sql);
            if (params != null){
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }
            result = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }


}
